﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Restaurant.BUS;
using Restaurant.DTO;

namespace Restaurant
{
    public partial class frmThemNhaCungCap : Form
    {
        public frmThemNhaCungCap()
        {
            InitializeComponent();
        }

        private NhaCungCap ncc = new NhaCungCap();
        public bool fix;
        public NhaCungCap Ncc
        {
            get { return ncc; }
            set { ncc = value; }
        }

        public int flag = 0;

        private NhaCungCapService nccService = new NhaCungCapService();

        public void LoadHanThanhToan()
        {
            DataTable dbtab = nccService.LoadHanThanhToan();
            //cb_htt.Text = "Tất Cả";
            //cb_htt.Items.Add("Tất Cả");
            for (int i = 0; i < dbtab.Rows.Count; i++)
            {
                DataRow dbdow = dbtab.Rows[i];
                cb_htt.Items.Add(dbdow[1].ToString());
            }
            cb_htt.Text = cb_htt.Items[0].ToString();
        }

        private void frmThemNhaCungCap_Load(object sender, EventArgs e)
        {
            if (flag == 1)
            {
                txt_tenncc.Text = Ncc.TenNCC;
                txt_diachi.Text = Ncc.Diachi;
                mtxt_sdt.Text = Ncc.Sdt;
                cb_htt.Text = nccService.TimMaHTTTheoMa(Ncc.MaHTT);
            }
            LoadHanThanhToan();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (flag == 0)
            {
                //NhaCungCap ncc = new NhaCungCap();
                ncc.TenNCC = txt_tenncc.Text;
                ncc.Diachi = txt_diachi.Text;
                ncc.Sdt = mtxt_sdt.Text.ToString();
                int index = cb_htt.SelectedIndex;
                ncc.MaHTT = nccService.TimMaHTTTheoTen(cb_htt.Items[index].ToString());
                if (nccService.ThemNhaCungCap(ncc))
                {
                    MessageBox.Show("Thêm Nhà Cung Cấp Thành Công");
                    this.Close();
                    return;
                }
                MessageBox.Show("Thêm Nhà Cung Cấp Thất Bại");
                return;
            }
            else
            {
                Ncc.TenNCC = txt_tenncc.Text;
                Ncc.Diachi = txt_diachi.Text;
                Ncc.Sdt = mtxt_sdt.Text;
                Ncc.MaHTT = nccService.TimMaHTTTheoTen(cb_htt.Text);
                bool kq;
                if (fix)
                    kq = nccService.SuaNCCDirtyread(ncc);
                else
                    kq = nccService.SuaNCCDirtyreadError(ncc);

                if (kq)
                {
                    MessageBox.Show("Thành Công");
                    this.Close();
                    return;
                }
                else
                {
                    MessageBox.Show("Thất bại");
                    return;
                }
            }
        }

        private void btnTroVe_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
